<?php
namespace Admin\Controller;
use Think\Controller;


/**
 * 后台日志控制器
 * Class LogController
 * @package Admin\Controller
 */
class LogController extends BaseController{
		//展示用户日志
		function user_log(){

            // 导入分页类
            import('Org.Util.Page');

            $name=I("post.name");
            //按用户名搜索

            if($name){
                //查询满足满足条件的总记录条数
                $count=M("user_log")->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->where("a.name='".$name."'")->order("id desc")->select();
                $count=count($count);
                //实例化分页类 传入总记录数和每页显示的记录数
                $num=10;
                $Page = new \Think\Page($count,$num);
                $Page->rollPage=5;
                //分页显示输出
                $show = $Page->show();
                $data=M("user_log")->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->where("a.name='".$name."'")->order("id desc")->limit($Page->firstRow,$Page->listRows)->select();

            }else{
                //查询满足满足条件的总记录条数
                $count=M("user_log")->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->order("id desc")->select();
                $count=count($count);
                //实例化分页类 传入总记录数和每页显示的记录数
                $num=10;
                $Page = new \Think\Page($count,$num);
                $Page->rollPage=5;
                //分页显示输出
                $show = $Page->show();
                $data=M("user_log")->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->order("id desc")->limit($Page->firstRow,$Page->listRows)->select();

            }
                $arr1=M("userbase")->field("name")->select();
                $this->assign("arr",$data);
                $this->assign('page',$show);
                $this->assign("arr1",$arr1);
                //输出总页数
                $this->assign("countnum",$Page->totalPages);
                $this->display();
		}
		//展示管理员日志
        function admin_log(){
            // 导入分页类
            import('Org.Util.Page');

		    $username=I("post.username");
            //按用户名搜索
            if($username){
                //查询满足满足条件的总记录条数
                $count=M("admin_log")->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->where("a.username='".$username."'")->order("w.id desc")->select();
                $count=count($count);
                //实例化分页类 传入总记录数和每页显示的记录数
                $num=10;
                $Page = new \Think\Page($count,$num);
                $Page->rollPage=5;
                //分页显示输出
                $show = $Page->show();
                $data=M("admin_log")->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->where("a.username='".$username."'")->order("w.id desc")->limit($Page->firstRow,$Page->listRows)->select();

            }else{
                //查询满足满足条件的总记录条数
                $count=M("admin_log")->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->order("w.id desc")->select();
                $count=count($count);
                //实例化分页类 传入总记录数和每页显示的记录数
                $num=30;
                $Page = new \Think\Page($count,$num);
                $Page->rollPage=5;
                //分页显示输出
                $show = $Page->show();
                $data=M("admin_log")->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->order("w.id desc")->limit($Page->firstRow,$Page->listRows)->select();
            }
            $arr1=M("admin")->field("username")->select();
            $this->assign("arr",$data);
            $this->assign('page',$show);
            $this->assign("arr1",$arr1);
            //输出总页数
            $this->assign("countnum",$Page->totalPages);
            $this->display();
        }
        //删除日志
        function delete()
        {    $id="";
            $arr=I("post.select");
            $tableName=I("post.tableName");
            foreach($arr as $v){
                $id.=$v.",";
            }
            $id=trim($id,",");
            $re=M("$tableName");
            $re->where("id in($id)")->delete();
            echo $re->getLastSql();
        }
        //导出日志
        function export(){
            $id="";
            $content="";
            $arr=I("post.select");
            $tableName=I("post.tableName");
            foreach($arr as $v){
                $id.=$v.",";
            }

            $id=trim($id,",");
            $re=M("$tableName");
               //导出来自管理员的日志
               if($tableName=='admin_log'){
                   if($arr==''){
                       $arr1=$re->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->select();
                   }else{
                       $arr1=$re->field("w.id,username,w.aid,addr,action,w.create_time")->alias("w")->join("__ADMIN__ as a on a.aid=w.aid")->where("w.id in($id)")->select();
                   }
                   foreach($arr1 as $vv){
                       $content.=$vv['id']."-".$vv['username']."-".$vv['addr']."-".$vv['action']."-".$vv['create_time']."\n";

                   }
               //导出来自用户的日志
               }else{
                   if($arr==''){
                       $arr1=$re->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->select();
                   }else{
                       $arr1=$re->field("id,name,w.uid,addr,action,w.create_time")->alias("w")->join("__USERBASE__ as a on a.uid=w.uid")->where("id in($id)")->select();
                   }
                   foreach($arr1 as $vv){
                       $content.=$vv['id']."-".$vv['name']."-".$vv['addr']."-".$vv['action']."-".$vv['create_time']."\n";

                   }
               }


            $files="./$tableName.txt";
           echo  file_put_contents($files,$content);
           echo "<script>
                    if(confirm('导出成功!导出文件在项目根目录名称为$tableName.txt')){
                        window.location.href='".__APP__."/Log/$tableName'
                    }else{
                        window.location.href='".__APP__."/Log/$tableName'
                    }
                </script>";
        }

}
